# ======================================================================
#
# Copyright (C) ADN
# ADN::LIBRARY::SOAP::ClientSOAP
# Description:
#  - Client pour faire de la communication SOAP
#  - Client to established SOAP Communicaion
#
# ======================================================================

package ADN::LIBRARY::SOAP::ClientSOAP;

use strict;
use ADN::SERVICE::Service;
use SOAP::Lite +trace => 'debug';

# #######################
# CREAT OBJECT
# #######################
sub new
{
	my $self = shift;
	my $class = ref($self) || $self;
	return $self if ref $self;
	$self = bless {} => $class;
	(%{$self->{param}}) = @_;
 	return $self->my_client();
}

# #######################
# MAKE MANY FLUX
# #######################
sub multiFlux {
    my $self = shift;
  	my $tmp ;
  	my @data;
  	foreach ( @{$self->{param}{Data}} ) {
    	$tmp .= $_;
  	}
  	push @data, SOAP::Data->type( '' )->name( 'Param' )->value( $tmp );
    undef $tmp;
  	return @data;
}

# #######################
# GET THE NAME OF THE FIRST BALISE
# #######################
sub nameFirst {
  	my $xml = $_[0];
  	$xml =~ s/^\<([^\>]+)\>.*/$1/smx;
  	return $xml;
}

# #######################
# ERASE THE FIRST AND LAST BALISE
# #######################
sub cleanFirst {
    my $xml = $_[0];
    $xml =~ s/^\<[^\>]+\>|\<\/[^\>]+\>$//gsmx;
    return $xml;
}

# #######################
# COMUNICATE IN SOAP
# #######################
sub my_client {
    my $self = shift;
  	my ( $soap, $meth, $gid );
  	my ( @tab, @head, @data );
  	my %service;
  	@tab = split /\//smx, $self->{param}{Service};
  	$gid = $tab[2];
  	%service = ADN::SERVICE::Service->new()->$gid( $tab[3] );
  	$meth = $service{response};
  	undef %service;
  	@head = ( SOAP::Header->type( '' )->name( 'IpServices' )->value( $self->{param}{Proxy} ),
		          SOAP::Header->type( '' )->name( 'IdClient' )->value( $tab[0] ),
	    	      SOAP::Header->type( '' )->name( 'PFct' )->value( $tab[1] ) );
  	if ( scalar @{$self->{param}{Data}} > 1 ) {
    	@data = $self->multiFlux( );
  	}
 	  else {
    	@data = SOAP::Data->type( '' )->name( 'Param' )->value( @{$self->{param}{Data}} );
  	}
  	$SOAP::Constants::PREFIX_ENV = 'soap';
  	$SOAP::Constants::PREFIX_ENC = 'soap';
  	$soap = SOAP::Lite->uri( 'urn://ADN-URI/' )
    				                ->proxy( $self->{param}{Proxy} )
    				                ->encoding( undef )
    				                ->autotype( 0 )
    				                ->readable( 1 )
    				                ->soapversion( 1.1 );
  	$soap->$meth( @head, @data );
  	undef $soap;
    return;
}

# #######################
# DESTROY OBJECT
# #######################
sub DESTROY {
    my $self = shift;
    return;
}
1;
__END__






































































































































